import * as api from '@/api'
// import { toYYYYMMDD } from './timeUtil'

/**
* 重构炸锅
*/
export const generateFryerDataList = (deviceType, data) => {
  let fixedDataArray = []
  console.log(deviceType, data)
  // 有数据
  if (data) {
    if (deviceType === '2001') {
      let recipeName = data.lVatStates === 2 ? fryerGetCookRecipe(data, 'lCook') : ''
      fixedDataArray.push({
        vats: '左槽',
        temp: data.lActualTem,
        state: data.lVatStates,
        isCook: data.lIsCook,
        cookState: recipeName,
        remainTime: ''
      })

      let recipeNameR = data.rVatStates === 2 ? fryerGetCookRecipe(data, 'rCook') : ''
      fixedDataArray.push({
        vats: '右槽',
        temp: data.rActualTem,
        state: data.rVatStates,
        isCook: data.rIsCook,
        cookState: recipeNameR,
        remainTime: ''
      })
      let recipeNameM = data.cVatStates === 2 ? fryerGetCookRecipe(data, 'cCook') : ''
      fixedDataArray.push({
        vats: '中间槽',
        temp: data.cActualTem,
        state: data.cVatStates,
        isCook: data.cIsCook,
        cookState: recipeNameM,
        remainTime: ''
      })
    }

    // 2017
    if (deviceType === '2017') {
      let recipeName = data.lVatStates === 2 ? fryerGetCookRecipe(data, 'lCook') : ''

      fixedDataArray.push({
        vats: '左槽1',
        state: data.lVatStates,
        temp: data.lActualTem,
        isCook: data.lIsCook,
        cookState: recipeName,
        remainTime: ''
      })

      let recipeNameM = data.cLVatStates === 2 ? fryerGetCookRecipe(data, 'cLCook') : ''

      fixedDataArray.push({
        vats: '左槽2',
        temp: data.cLActualTem,
        state: data.cLVatStates,
        isCook: data.cLIsCook,
        cookState: recipeNameM,
        remainTime: ''
      })

      let recipeNameR = data.rVatStates === 2 ? fryerGetCookRecipe(data, 'rCook') : ''

      fixedDataArray.push({
        vats: '右槽1',
        temp: data.rActualTem,
        state: data.rVatStates,
        isCook: data.rIsCook,
        cookState: recipeNameR,
        remainTime: ''
      })

      let recipeNameML = data.cRVatStates === 2 ? fryerGetCookRecipe(data, 'cCook') : ''
      fixedDataArray.push({
        vats: '右槽2',
        temp: data.cRActualTem,
        state: data.cRVatStates,
        isCook: data.cRIsCook,
        cookState: recipeNameML,
        remainTime: ''
      })
    }

    // 2018
    if (deviceType === '2018') {
      let recipeName = data.lVatStates === 2 ? fryerGetCookRecipe(data, 'lCook') : ''
      fixedDataArray.push({
        vats: '左槽',
        temp: data.lActualTem,
        state: data.lVatStates,
        isCook: data.lIsCook,
        cookState: recipeName,
        remainTime: ''
      })
      let RrecipeName = data.rVatStates === 2 ? fryerGetCookRecipe(data, 'rCook') : ''
      fixedDataArray.push({
        vats: '右槽',
        temp: data.rActualTem,
        state: data.rVatStates,
        isCook: data.rIsCook,
        cookState: RrecipeName,
        remainTime: ''
      })

      console.log(this.fryerFixedData)
    }

    // "actualTem": 310,
    // "error": 0,
    // "isCook": 0,
    // "misc": 0,
    // "misc2": 0,
    // "setTem": 0,
    // "vatStates": 2
    // 2019
    if (deviceType === '2019') {
      let recipeName = data.vatStates === 2 ? fryerGetCookRecipe(data, 'cook') : ''
      fixedDataArray.push({
        vats: '槽',
        temp: data.actualTem,
        state: data.vatStates,
        isCook: data.isCook,
        cookState: recipeName,
        remainTime: ''
      })
    }
  } else {
    // data === null
    if (deviceType === '2001') {
      let vats = ['左槽', '右槽', '中间槽']
      for (let vat of vats) {
        fixedDataArray.push({
          vats: vat,
          temp: '-',
          state: '-',
          isCook: '-',
          cookState: '-',
          remainTime: '-'
        })
      }
    }

    // 2017
    if (deviceType === '2017') {
      let vats = ['左槽1', '左槽2', '右槽1', '右槽2']
      for (let vat of vats) {
        fixedDataArray.push({
          vats: vat,
          temp: '-',
          state: '-',
          isCook: '-',
          cookState: '-',
          remainTime: '-'
        })
      }
    }

    // 2018
    if (deviceType === '2018') {
      let vats = ['左槽', '右槽']
      for (let vat of vats) {
        fixedDataArray.push({
          vats: vat,
          temp: '-',
          state: '-',
          isCook: '-',
          cookState: '-',
          remainTime: '-'
        })
      }
      console.log(this.fryerFixedData)
    }

    // "actualTem": 310,
    // "error": 0,
    // "isCook": 0,
    // "misc": 0,
    // "misc2": 0,
    // "setTem": 0,
    // "vatStates": 2
    // 2019
    if (deviceType === '2019') {
      fixedDataArray.push({
        vats: '槽',
        temp: '-',
        state: '-',
        isCook: '-',
        cookState: '-',
        remainTime: '-'
      })
    }
  }

  return fixedDataArray
}

function fryerGetCookRecipe (data, key) {
  // data.cook.recipeName
  if (data && data.hasOwnProperty(key) && data[key] && data[key].hasOwnProperty('recipeName') && data[key].recipeName) {
    return data[key].recipeName
  }
}

/**
 * 给报表排序
 * @param {*} r Array
 * dedup 是否按日期去重
 */
export const sortReportData = (r, dedup = true) => {
  let chartData = []
  /**
     *  生成每天 最新回温
     */
  const afterSort = r.sort((a, b) => {
    let aTs = new Date(a.create_time).getTime()
    let bTs = new Date(b.create_time).getTime()
    return aTs - bTs
  })
  console.log('after sort>>', afterSort)

  // 按日期去重
  for (let i = 0; i < afterSort.length; i++) {
    const item = afterSort[i]
    const existItem = chartData.find(e => {
      return e.day === item.day
    })
    if (existItem) {
      let itemCreateTime = new Date(item.create_time).getTime()
      let existCreateTime = new Date(existItem.create_time).getTime()
      if (itemCreateTime > existCreateTime) {
        chartData.pop()
        chartData.push(item)
      }
    } else {
      chartData.push(item)
    }
  }

  if (dedup) {
    return chartData
  } else {
    return afterSort
  }
}

/**
* 全时段 未处理 离线告警
*/
export const getOfflineAlarm = (serial, deviceType, channel, psid, role, user_psid) => {
  return getOfflineAlarmList(serial, deviceType, channel, psid, role, user_psid)
}

async function getOfflineAlarmList (serial, deviceType, channel, psid, role, user_psid) {
  const res = await alarmList(serial, deviceType, channel, psid, role, user_psid)
  const res1 = await alarmList1(serial, deviceType, channel, psid, role, user_psid)

  const r = res.data.data
  const r1 = res1.data.data

  console.log(r)
  let isOffline = false
  let reason = ''
  let isOffline1 = false

  let isFryer = ['2001', '2017', '2018']

  if (r.length > 0) {
    for (let i = 0; i < r.length; i++) {
      const item = r[i]

      if (isFryer.indexOf(deviceType) >= 0) {
        if (item.except_reason.indexOf('掉线') >= 0 && item.except_reason.indexOf('左') < 0 && item.except_reason.indexOf('右') < 0 && item.except_reason.indexOf('中') < 0) {
          isOffline = true
          break
        } else if (item.except_reason.indexOf('掉线') >= 0) {
          isOffline = true
          reason += item.except_reason
        } else if (item.except_reason.indexOf('与网关通讯中断') >= 0) {
          isOffline = true
          break
        }
      } else if (item.except_reason.indexOf('与网关通讯中断') >= 0 || item.except_reason.indexOf('掉线') >= 0) {
        isOffline = true
        break
      }
    }
  }
  if (r1.length > 0) {
    for (let i = 0; i < r1.length; i++) {
      const item = r1[i]
      if (item.restore_type === 1) {
        if (isFryer.indexOf(deviceType) >= 0) {
          if (item.except_reason.indexOf('掉线') >= 0 && item.except_reason.indexOf('左') < 0 && item.except_reason.indexOf('右') < 0 && item.except_reason.indexOf('中') < 0) {
            isOffline1 = true
            break
          } else if (item.except_reason.indexOf('掉线') >= 0) {
            isOffline1 = true
          } else if (item.except_reason.indexOf('与网关通讯中断') >= 0) {
            isOffline1 = true
            break
          }
        } else if (item.except_reason.indexOf('与网关通讯中断') >= 0 || item.except_reason.indexOf('掉线') >= 0) {
          isOffline1 = true
          break
        }
      }
    }
  }

  // 炸锅另外处理
  if (deviceType === '2001' || deviceType === '2017' || deviceType === '2018' || deviceType === '2019') {
    if (isOffline) {
      return {
        isOffline: true,
        isUserAction: false,
        reason: reason
      }
    } else {
      // 人工处理的告警
      if (isOffline1) {
        return {
          isOffline: false,
          isUserAction: true
        }
      } else {
        return {
          isOffline: false,
          isUserAction: false
        }
      }
    }
  } else {
    return isOffline
  }
}

function alarmList (serial, deviceType, channel, psid, role, user_psid) {
  return api.device
    .yumAlarmList({
      serial: serial,
      device_type: deviceType,
      status: [],
      time_start: null,
      time_end: null,
      except_level: [],
      repair_status: [0, 1],
      channel: channel,
      psid: psid,
      role: role,
      user_psid: user_psid,
      groups: [],
      offset: 0,
      limit: 1000000
    })
}

// eslint-disable-next-line no-unused-vars
function alarmList1 (serial, deviceType, channel, psid, role, user_psid) {
  return api.device
    .yumAlarmList({
      serial: serial,
      device_type: deviceType,
      status: [],
      time_start: null,
      time_end: null,
      except_level: [],
      repair_status: [4],
      channel: channel,
      psid: psid,
      role: role,
      user_psid: user_psid,
      groups: [],
      offset: 0,
      limit: 1
    })
}
